package org.minuframework.utils;

import java.util.Random;

import org.minuframework.lang.StringUtils;

public class CodeUtils {
    
    private static final int DEFAULT_SIZE = 4;
    
    public static final char[] params = {
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a',
            'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2',
            '3', '4', '5', '6', '7', '8', '9', '0'
    };
    
    public static String randomCode(int size, boolean number, boolean lower) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < size; i++) {
            Random ran = new Random();
            int key = ran.nextInt(params.length - 1);
            if (number) {
                if (!StringUtils.isNumCase(params[key])) {
                    while (!StringUtils.isNumCase(params[key])) {
                        key = ran.nextInt(params.length - 1);
                    }
                }
            }
            if (lower) {
                if (StringUtils.isUpperCase(params[key])) {
                    while (StringUtils.isUpperCase(params[key])) {
                        key = ran.nextInt(params.length - 1);
                    }
                }
            }
            result.append(params[key]);
        }
        return result.toString();
    }
    
    public static String randomCode(boolean number, boolean lower) {
        return randomCode(DEFAULT_SIZE, number, lower);
    }
    
    public static String randomCode() {
        return randomCode(DEFAULT_SIZE, false, false);
    }
    
}
